package pers.liyan.shi.springcloud.producerticketclient.client.hystrix;

import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import pers.liyan.shi.springcloud.producerticketclient.client.TicketClient;
import pers.liyan.shi.springcloud.producerticketclient.domain.Ticket;

import java.util.Date;

/**
 * 调用微服务失败回退, 实现 FallbackFactory 可以获取到失败的原因
 *  也需要使用 @Component 注解
 * @author shily
 * @date 2019/4/21 14:27
 */
@Component
public class HystrixClientFallbackFactory implements FallbackFactory<TicketClient> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public TicketClient create(Throwable throwable) {
        return new TicketClient(){

            @Override
            public Ticket buyTicket(String movieName) {
                logger.warn("触发熔断, " + throwable.toString());
                return new Ticket(throwable.toString(), new Date(), null);
            }
        };
    }
}
